define([ './module' ], function(module) {
	'use strict';


	module.controller('memberdeliveraddress',servermemberdeliveraddressctrl);
	function servermemberdeliveraddressctrl($rootScope,$stateParams,$http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder,Upload,$timeout,GoodsTypeService) {

		var vm = this;
		vm.message = '';
		vm.someClickHandler = someClickHandler;
		vm.selected = {};
		vm.selectAll = false;
		vm.toggleAll = toggleAll;
		vm.toggleOne = toggleOne;
		vm.dtInstance = {};
		var titleHtml = '<input type="checkbox" ng-model="gt.selectAll" ng-click="gt.toggleAll(gt.selectAll,gt.selected)">';
		vm.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
				type: 'POST',
				url: '../mall/member/findByPage',
				dataSrc: 'data'
			})
			.withOption('processing', true)
			.withOption('paging', true)
			.withOption('serverSide', true)
			.withOption('createdRow', function (row, data, dataIndex) {
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function (header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback', rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest', function () {

		});
		vm.dtColumns = [
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function (data, type, full, meta) {
				vm.selected[full.id] = false;
				return "<input type='checkbox' ng-model=\"gt.selected['" + data.id + "']\" ng-click=\"gt.toggleOne(gt.selected)\">";
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			DTColumnBuilder.newColumn('loginname').withTitle('登录名').notSortable(),
			DTColumnBuilder.newColumn('nick').withTitle('昵称').notSortable(),
			DTColumnBuilder.newColumn('sex').withTitle('性别').notSortable(),
			DTColumnBuilder.newColumn('mobile').withTitle('手机号').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable()];
		function imgHtml(data, type, full, meta) {
			return '<img style="width:50px;height:50px;" ng-src="' + data.mainimg + '" alt="Photo" />';
		}

		function reloadData() {
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}

		function callback(data) {
		}


		/**
		 * 列表全选
		 */
		function toggleAll(selectAll, selectedItems) {
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					selectedItems[id] = selectAll;
				}
			}
		}

		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems) {
			var me = this;
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					if (!selectedItems[id]) {
						me.selectAll = false;
					}
				}
			}
			me.selectAll = true;

		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
			$scope.pushToSelectedProducts(info.id, info);
		}

		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function () {
				$scope.$apply(function () {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}


		//选定的专题货物

		$scope.pushToSelectedProducts = function (index, member) {
			$scope.sp=[];
			$scope.sp.push(member);
			$scope.selectedmembers=$scope.sp;
		};


		/**
		 * 删除选择的会员
		 * @param index
		 */
		$scope.removeselectmember = function (index) {
			$scope.selectedmembers.splice(index, 1);
			$scope.sp.slice(index,1);
		}



		/**
		 *  查询所有省份
		 */
		$scope.findallshengfen=function(){
			$http({
				method:'POST',
				url:'../mall/basicregion/findRegionByType',
				data:{
					'type':"0"
				}
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.shengfenall=data.beanlist;
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		}

		/**
		 *  查询省份下的市
		 */
		$scope.findallshibyshengfen=function(){
			$http({
				method:'POST',
				url:'../mall/basicregion/findRegionShiByShengfen',
				data:{
					'parentid':$scope.shengfenallx.id
				}
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.shiall=data.beanlist;
					$scope.shishow=true;
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		}

		/**
		 *  查询市下的区
		 */
		$scope.findallqubyshi=function(){
			$http({
				method:'POST',
				url:'../mall/basicregion/findRegionShiByShengfen',
				data:{
					'parentid':$scope.shiallx.id
				}
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.quall=data.beanlist;
					$scope.qushow=true;
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		}




		/**
		 * 省份选择改变
		 */
		$scope.changeshenfen=function(){
			if($scope.shengfenallx==undefined){
				$scope.errors={
					show:true,
					msg:"请选择省份"
				}
				return false;
			}
			$scope.findallshibyshengfen();
		}
		/**
		 * 市选择改变
		 */
		$scope.changeshi=function(){
			if($scope.shiallx==undefined){
				$scope.errors={
					show:true,
					msg:"请选择市"
				}
				return false;
			}
			$scope.findallqubyshi();
		}






		var operate = $location.search().operate || $stateParams.operate;
		if (operate != undefined && operate == "save") {
			$scope.title = "添加会员收获地址信息";
			//控制保存按钮显示
			$scope.savebtn = {show: true};
			//设置启用 1表示启用
			$scope.status = "1";
			$scope.isdefault="0";
			$scope.tag="0";
			$scope.areashow=true;
			$scope.addressshow=false;
			$scope.modifyarashow=false;
			$scope.membershow=true;
			$scope.save = function () {
				if($scope.selectedmembers.length==0){
					$scope.errors = {
						show: true,
						msg: '请选择会员'
					}
					return false;
				}

				if ($scope.consignee ==undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入收件人'
					}
					return false;
				}
				if ($scope.shengfenallx == undefined) {
					$scope.errors = {
						show: true,
						msg: '请选择省份'
					}
					return false;
				}

				if ($scope.shiallx == undefined) {
					$scope.errors = {
						show: true,
						msg: '请选择市'
					}
					return false;
				}
				//if ($scope.quallx == undefined) {
				//	$scope.errors = {
				//		show: true,
				//		msg: '请选择区'
				//	}
				//	return false;
				//}
				if ($scope.street == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入街道'
					}
					return false;
				}

				if ($scope.detail == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入地址'
					}
					return false;
				}

				if ($scope.postcode == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入邮编'
					}
					return false;
				}

				if ($scope.mobile == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入手机'
					}
					return false;
				}


				if ($scope.telno == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入座机'
					}
					return false;
				}

				if ($scope.email == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入邮箱'
					}
					return false;
				}

				$http({
					method: 'POST',
					url: '../mall/memberdeliveraddress/save',
					data: {
						'memberid': $scope.selectedmembers[0].id,
						'city': $scope.shiallx.name,
						'consignee':$scope.consignee,
						'country':'中国',
						'detail': $scope.detail,
						'district':$scope.district,
						'email':$scope.email,
						'isdefault':$scope.isdefault,
						'mobile':$scope.mobile,
						'postcode':$scope.postcode,
						'province':$scope.shengfenallx.name,
						'street':$scope.street,
						'tag':$scope.tag,
						'telno':$scope.telno,
						'status': $scope.status
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.info = {
							show: true,
							msg: '会员收获地址添加成功'
						}
						$location.path('/memberdeliveraddressment');
					} else {
						$scope.errors = {
							show: true,
							msg: data.msg
						}
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}
		}

		if (operate == 'edit') {
			var id = $location.search().id;
			if (id != undefined && id != "") {
				$http({
					method: 'POST',
					url: '../mall/memberdeliveraddress/find',
					data: {
						'id': id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.title = '正在对收获地址进行编辑';
						$scope.consignee=data.bean.consignee;
						$scope.street = data.bean.street;
						$scope.detail = data.bean.detail;
						$scope.postcode=data.bean.postcode;
						$scope.mobile=data.bean.mobile;
						$scope.telno=data.bean.telno;
						$scope.email=data.bean.email;
						$scope.tag=data.bean.tag;
						$scope.isdefault=data.bean.isdefault;
						$scope.status = data.bean.status;
						$scope.addressshow=true;
						$scope.address=data.bean.country+data.bean.province+data.bean.city+data.bean.district+data.bean.street+data.bean.detail;
						$scope.province=data.bean.province;
						$scope.city=data.bean.city;
						$scope.district=data.bean.district;
						$scope.areashow=false;
						$scope.modifyarashow=true;
						$scope.membershow=false;
						$scope.updatebtn = {show: true};
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}


			$scope.clickmodifyaddress=function(){
				$scope.areashow=true;
			}

			$scope.update = function () {
				if ($scope.consignee ==undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入收件人'
					}
					return false;
				}
				if($scope.areashow){
					var province="";
					var city="";
					var district="";
					if ($scope.shengfenallx == undefined) {
						$scope.errors = {
							show: true,
							msg: '请选择省份'
						}
						return false;
					}else{
						province=$scope.shengfenallx.name;
					}

					if ($scope.shiallx == undefined) {
						$scope.errors = {
							show: true,
							msg: '请选择市'
						}
						return false;
					}else{
						city=$scope.shiallx.name;
					}
					//if ($scope.quallx == undefined) {
					//	$scope.errors = {
					//		show: true,
					//		msg: '请选择区'
					//	}
					//	return false;
					//}else{
					//	district=$scope.quallx.name;
					//}
				}else{
					province=$scope.province;
					city=$scope.city;
					district=$scope.district;
				}

				if ($scope.street == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入街道'
					}
					return false;
				}

				if ($scope.detail == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入地址'
					}
					return false;
				}

				if ($scope.postcode == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入邮编'
					}
					return false;
				}

				if ($scope.mobile == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入手机'
					}
					return false;
				}


				if ($scope.telno == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入座机'
					}
					return false;
				}

				if ($scope.email == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入邮箱'
					}
					return false;
				}


				$http({
					method: 'POST',
					url: '../mall/memberdeliveraddress/update',
					data: {
						'id': id,
						'city': city,
						'consignee':$scope.consignee,
						'country':'中国',
						'detail': $scope.detail,
						'district':district,
						'email':$scope.email,
						'isdefault':$scope.isdefault,
						'mobile':$scope.mobile,
						'postcode':$scope.postcode,
						'province':province,
						'street':$scope.street,
						'tag':$scope.tag,
						'telno':$scope.telno,
						'status': $scope.status
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.info = {
							show: true,
							msg: '会员地址更新成功'
						}
						$location.path('/memberdeliveraddressment').search({'operate':'edit','albumtitle':$rootScope.gloalbumtitle,'id':$rootScope.glopromotionProductAlbumId});

				} else {
						$scope.errors = {
							show: true,
							msg: data.msg
						}
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

		}
	}



	module.controller('memberdeliveraddressment',servermemberdeliveraddressmentctrl);
	//查询列表数据
	function servermemberdeliveraddressmentctrl($rootScope,$http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder){
		$scope.title='搜索会员的收获地址';
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=
			DTOptionsBuilder.newOptions().withOption('ajax',{
					type:'POST',
					url:'../mall/memberdeliveraddress/findByPage',
					dataSrc:'data'
				})
				.withOption('processing',false)
				.withOption('paging',true)
				.withOption('serverSide',true)
				.withOption('createdRow',function(row,data,dataIndex){
					$compile(angular.element(row).contents())($scope);
				})
				.withOption('headerCallback', function(header) {
					if (!$scope.headerCompiled) {
						$scope.headerCompiled = true;
						$compile(angular.element(header).contents())($scope);
					}
				})
				.withOption('stateSave', true)
				.withOption('rowCallback',rowCallback)
				.withPaginationType('full_numbers')
				.withLanguageSource('./app/language/chinese.json')
		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
				vm.selected[full.id]=false;
				return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn('consignee').withTitle('收件人').notSortable(),
			DTColumnBuilder.newColumn('address').withTitle('地址').notSortable(),
			DTColumnBuilder.newColumn('mobile').withTitle('电话').notSortable(),
			DTColumnBuilder.newColumn('tag').withTitle('标签').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="edit('+data.id+')"><i class="fa fa-edit"></i></button>';
		}

		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/memberdeliveraddress').search({'operate':'edit','id':id});
		}


		$scope.save=function(){
			$location.path('/memberdeliveraddress').search({'operate':'save'});
		}

		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
					show:true,
					msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
					show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/memberdeliveraddress/del',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
							show:true,
							msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
		}
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function() {
				$scope.$apply(function() {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}



	}
});
